/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: managerLogin.c
> Author: KAIJIE.JIANG
> Mail: 
> Created Time: 2024年07月17日 星期三 20时30分03秒
***************************************************/

#include "managerLogin.h"

//创建头节点
ML * createManagerLoginHead(void){
	ML *head = NULL;
	head = (ML*)malloc(sizeof(ML));
	memset(head,0,sizeof(ML));
	return head;
}

//增加链表
ML * addManagerLoginLink(ML *head){
	assert(head);
	ML *cur = NULL;
	ML *new = NULL;
	cur = head;
	char n[20] = {0};


	char str[20] = {"1"};
	char dest[20] = {0};

	printf("管理员账号注册\n");
	printf("请输入管理员账号操作密码：");
	scanf("%s",dest);

	if(strcmp(str,dest) == 0){
		new = (ML*)malloc(sizeof(ML));
		memset(new,0,sizeof(ML));

		printf("管理员账号名：");
		scanf("%s",n);
		while(cur->next != NULL){
			if(strcmp(cur->data.managerName,n) == 0){
				printf("管理员账号已存在！\n");
				return head;
			}
			cur = cur->next;
		}
		strcpy(new->data.managerName,n);
		printf("管理员密码：");
		scanf("%s",new->data.managerPassword);

		cur->next = new;
		new->next = NULL;
		printf("管理员账号注册成功！\n");
	}else{
		printf("密码错误！\n");
	}

	return head;
}

//输出链表
void outputManagerLoginLink(const ML *head){
	assert(head);
	ML * tmp = NULL;
	tmp = head->next;

	while(tmp != NULL){
		printf("managerName:%s\tmanagerPassword:%s\n",tmp->data.managerName,tmp->data.managerPassword);
		tmp = tmp->next;
	}
}

//修改链表
ML * amendManagerLoginLink(ML * head){
	ML * cur = NULL;
	cur = head->next;

	char s[20] = {"1"};
	char s1[20] = {0};
	char str[20] = {0};
	char dest[20] = {0};
	int flag = 0;

	printf("请输入管理员账号操作密码：");
	scanf("%s",s1);
	if(strcmp(s,s1) == 0){
		printf("修改密码\n");
		printf("管理员账号：");
		scanf("%s",str);

		while(cur != NULL){
			if(strcmp(cur->data.managerName,str) == 0){
				flag = 1;
				while(1){
					bzero(dest,sizeof(dest));
					printf("输入旧密码：");
					scanf("%s",dest);
					if(strcmp(cur->data.managerPassword,dest) == 0){
						printf("输入新密码：");
						scanf("%s",cur->data.managerPassword);
						flag = 1;
						break;
					}else{
						printf("旧密码错误！\n");
					}
				}
				break;
			}
			cur = cur->next;
		}
		if(flag == 0){
			printf("未找到账号！\n");
		}
	}else{
		printf("密码错误！\n");
	}

	return head;
}


//释放链表
void freeManagerLoginLink(ML * head){
	assert(head!=NULL);
	
	ML *temp = NULL;
	temp = head;
	ML *cur = NULL;
	while(temp!=NULL)
	{
		//cur 存放下一个节点
		cur = temp->next;
		free(temp);
		temp = NULL;
		temp = cur;
	}
}

//将链表信息保存为文件
FILE * saveManagerLoginLinkToFile(FILE * fp,ML * head){
	ML *cur = NULL;
	cur = head->next;
	while(cur != NULL){
		fwrite(cur,sizeof(ML),1,fp);
	
		cur = cur->next;
	}
	cur = NULL;
	return fp;
}

//将文件读到链表
ML *readManagerLoginFileToLink(FILE *fp,ML *head){
	ML *cur = NULL;
	ML *new = NULL;
	cur = head;

	assert(head);
	size_t ld = 1;
	while(ld != 0){
		new = (ML*)malloc(sizeof(ML));
		memset(new,0,sizeof(ML));

		ld = fread(new,sizeof(ML),1,fp);
		if(ld == 0){
			free(new);
			new = NULL;
			break;
		}
//		printf("name:%s\t,managerPassword:%s\n",new->data.managerName,new->data.managerPassword);
		cur->next = new;
		new->next = NULL;
		cur = cur->next;
	}
	return head;
}

//输入用户名，查询，没找到重新输入，name有后判断密码是否正确 正确输出1，不正确输出0
int loginManagerSystem(ML *head){
	ML *cur = NULL;
	char str[20];
	char dest[20];

	int flag = 0;
	int flag1 = 0;
	while(1){
		//判断可否找到ML，找到flag1=1，找不到flag1=0
		bzero(str,20*sizeof(char));
		bzero(dest,20*sizeof(char));

		printf("管理员账号:");
		scanf("%s",str);
		cur = head->next;
		while(cur != NULL){
			if(strcmp(cur->data.managerName,str) == 0){
				flag1 = 1;
				break;
			}
			cur = cur->next;
		}
		if(flag1 == 1){
			//判断密码是否正确
			printf("管理员密码：");
			scanf("%s",dest);

			if(strcmp(cur->data.managerPassword,dest) == 0){
				flag = 1;
				printf("登录成功!\n");
				break;	
			}else{
				printf("密码输入错误!\n");
				continue;
			}
		}else{
			printf("未找到管理员账号\n");
		}
	}

	cur = NULL;
	return flag;
}
